﻿
$(document).ready(function () {
    //    $("#txt_frdate").datepicker({ dateFormat: 'dd/mm/yy' });
    //    $("#txt_todate").datepicker({ dateFormat: 'dd/mm/yy' });
    //    $("#txt_DateRecorded").datepicker({ dateFormat: 'dd/mm/yy' });
    //$("#plistdateto").datepicker({ dateFormat: 'dd/mm/yy' });
});

function fireParentForm(LevelLocationType, WebpartId, PermissionCode) {

}

//function SwitchLevelType(LevelLocationType, WebpartId, PermissionCode) {
//    if (LevelLocationType == "0") {
//        $("#_locationSelect").html("");
//        return;
//    }
//    var filter = Portal.SODC.IDIS.Locations.ServerSideRenderFilter(LevelLocationType, WebpartId, PermissionCode).value;
//    if (filter.indexOf("NONE") != -1) {
//        $("#_filterpanel").html("Không có dữ liệu lọc theo cấp trên.");
//        $("#_locationSelect").html(Portal.SODC.IDIS.Locations.LocationBuilder(LevelLocationType, WebpartId, PermissionCode).value);
//    }
//    else {
//        $("#_filterpanel").html(filter);
//    } 
//}

function checkNuber(num) {
    if (num != "") {
        if (isNaN(num) == true) {
            alert("Yêu cầu nhập số!");
            return false;
        }
        return true;
    }
    return true;
}

function ClientStatus() {
    alert("Chuyên mục này đang xây dựng!");
    return;
}


function ClientUpdateCrimeInfo(RecordId) {

    var daterecord = document.getElementById("drpDaytxt_DateRecorded").options[document.getElementById("drpDaytxt_DateRecorded").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_DateRecorded").options[document.getElementById("drpMonthtxt_DateRecorded").selectedIndex].value + '/' + document.getElementById("drpYeartxt_DateRecorded").options[document.getElementById("drpYeartxt_DateRecorded").selectedIndex].value;

    if (daterecord == "//") {
        alert("Yêu cầu nhập ngày tháng!!!");
        document.getElementById("drpDaytxt_DateRecorded").focus();
        return;
    } else {
        if (!CheckValue()) {
            return;
        } else {
            var CasePolice = document.getElementById("txt_CrimeCaseIOFByPolice").value;
            
            var CaseCustoms = document.getElementById("txt_CrimeCaseIOFByCustoms").value;
            

            var CaseMin = document.getElementById("txt_CrimeCaseIOFByMinistry").value;
            
            var CaseOther = document.getElementById("txt_CrimeCaseIOFByOther").value;
            

            var CrimePolice = document.getElementById("txt_CrimePersonIOFByPolice").value;
            

            var CrimeCustoms = document.getElementById("txt_CrimePersonIOFByCustoms").value;
            
            var CrimeMin = document.getElementById("txt_CrimePersonIOFByMinistry").value;
            

            var CrimeOther = document.getElementById("txt_CrimePersonIOFByOther").value;


            var LocationId = $("#_locationsel").val();

            if (typeof (LocationId) == 'undefined' || LocationId == null) {
                var LocationId_Curent = $("#LocationId_Curent").val();
                LocationId = LocationId_Curent;
            }
            RetValue = Portal.SODC.IDIS.EditCrimeInfo.ServerSideUpdateCrimeInfo(daterecord, LocationId, CasePolice, CaseCustoms, CaseMin, CaseOther, CrimePolice, CrimeCustoms, CrimeMin, CrimeOther, RecordId).value;
            if (RetValue.indexOf("ERROR") != -1) {
                alert(RetValue.replace("ERROR: ", ""));
                return;
            } else {
                alert(RetValue);
            }
        }
    }
}

function selectLevelTypeforInsertOfficials() {
    var ltype = $("#selectlevel").val();
    $("#SelectLocationZone").html(Portal.SODC.IDIS.AddNewOfficials.ServerSideRenderLocationSelect(ltype).value);
}

function selectLevelTypeforEditOfficials() {
    var ltype = $("#selectlevel").val();
    $("#SelectLocationZone").html(Portal.SODC.IDIS.EditOfficials.ServerSideRenderLocationSelect(ltype).value);
}

function selectLevelTypeforSearchOfficials() {
    var ltype = $("#selectlevel").val();
    $("#SelectLocationZone").html(Portal.SODC.IDIS.ListOfficials.ServerSideRenderLocationSelect(ltype).value);
}

function ClientDoAddCrimeInfo() {

    var locationId = $("#_locationsel").val();
    if (locationId == null || locationId == undefined) {
        alert("Bạn phải chọn cấp - địa phương để nhập dữ liệu !")
        return;
    } else if (locationId.length == 0 || locationId == 0) {
        alert("Bạn phải chọn cấp - địa phương để nhập dữ liệu !")
        return;
    }

    var daterecord = document.getElementById("drpDaytxt_DateRecorded").options[document.getElementById("drpDaytxt_DateRecorded").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_DateRecorded").options[document.getElementById("drpMonthtxt_DateRecorded").selectedIndex].value + '/' + document.getElementById("drpYeartxt_DateRecorded").options[document.getElementById("drpYeartxt_DateRecorded").selectedIndex].value;

    if (daterecord == "//") {
        alert("Yêu cầu nhập ngày tháng!");
        document.getElementById("drpDaytxt_DateRecorded").focus();
        return;
    } else {
        if (!CheckValue()) {
            return;
        } else {
            var CasePolice = document.getElementById("txt_CrimeCaseIOFByPolice").value;

            var CaseCustoms = document.getElementById("txt_CrimeCaseIOFByCustoms").value;

            var CaseMin = document.getElementById("txt_CrimeCaseIOFByMinistry").value;

            var CaseOther = document.getElementById("txt_CrimeCaseIOFByOther").value;


            var CrimePolice = document.getElementById("txt_CrimePersonIOFByPolice").value;

            var CrimeCustoms = document.getElementById("txt_CrimePersonIOFByCustoms").value;

            var CrimeMin = document.getElementById("txt_CrimePersonIOFByMinistry").value;

            var CrimeOther = document.getElementById("txt_CrimePersonIOFByOther").value;

            RetValue = Portal.SODC.IDIS.AddNewCrimeInfo.ServerSideAddNewCrimeInfo(
                                    daterecord, locationId,
                                    CasePolice, CaseCustoms,
                                    CaseMin, CaseOther,
                                    CrimePolice, CrimeCustoms,
                                    CrimeMin, CrimeOther).value;
            if (RetValue.indexOf("ERROR") != -1) {
                alert(RetValue.replace("ERROR: ", ""));
                return;
            } else {
                alert(RetValue);
                ResetAllField();
            }
        }
    }
}

function CheckValue() {

    var textBoxes = $("input[type='text']");
    for (var i = 0; i < textBoxes.length; i++) {
        value = textBoxes[i].value;
        if (value != '' && value != null) {
            if (!IsNumeric(value)) {
                alert("Yêu cầu nhập số!");
                textBoxes[i].focus();
                return false;
            }
        } else {
            textBoxes[i].value = 0;
        }
    }
    return true;
}


function IsNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}




function isValidDate(sText) {
    var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
    return reDate.test(sText);
}


function ResetAllField() {
    var textBoxes = $("input[type='text']");
    for (var i = 0; i < textBoxes.length; i++) {
        textBoxes[i].value = "";
    }
}

function ClientDoSimpleSearchCrimeInfo() {

    var LocationId = $("#_locationsel").val();

    frDate = document.getElementById("drpDaytxt_frdate").options[document.getElementById("drpDaytxt_frdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_frdate").options[document.getElementById("drpMonthtxt_frdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_frdate").options[document.getElementById("drpYeartxt_frdate").selectedIndex].value;
    toDate = document.getElementById("drpDaytxt_todate").options[document.getElementById("drpDaytxt_todate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_todate").options[document.getElementById("drpMonthtxt_todate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_todate").options[document.getElementById("drpYeartxt_todate").selectedIndex].value;
    //    if (frDate == "//") {
    //        frDate = null;
    //    }
    //    if (toDate == "//") {
    //        toDate = null;
    //    }

    result = document.getElementById("ContentSearchResult");
    if (LocationId) {
        document.getElementById("divProcessingSearchResult").innerHTML = "Hệ thống đang xử lý";
        ps = null;
        Ret = Portal.SODC.IDIS.ListCrimeInfo.GetResult('', LocationId, frDate, toDate, 0, "JumpPageSearchResultCrimeInfo", ps).value;
        if (Ret.indexOf("ERROR") != -1) {
            alert(Ret.replace("ERROR: ", ""));
            document.getElementById("divProcessingSearchResult").innerHTML = "";
            return;
        } else {
            result.innerHTML = Ret;
            document.getElementById("divProcessingSearchResult").innerHTML = "";
        }
    } else {
        alert("Chưa xác định được địa phương !");
        result.innerHTML = "";
    }

}

function JumpPageSearchResultCrimeInfo(wp, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    document.getElementById("divProcessingSearchResult").innerHTML = Portal.SODC.IDIS.ListCrimeInfo.GetProcessingText().value;
    setTimeout("RealJumpPageSearchResultCrimeInfo('','" + MaxItems + "', 20, 10, '" + CurrentPage + "', 'RealJumpPageSearchResultCrimeInfo')", 10);
}
function RealJumpPageSearchResultCrimeInfo(WebpartId, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    PageIndex = CurrentPage;
    var LocationId = document.getElementById("txt_LocationId").value;
    frDate = document.getElementById("drpDaytxt_frdate").options[document.getElementById("drpDaytxt_frdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_frdate").options[document.getElementById("drpMonthtxt_frdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_frdate").options[document.getElementById("drpYeartxt_frdate").selectedIndex].value;
    toDate = document.getElementById("drpDaytxt_todate").options[document.getElementById("drpDaytxt_todate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_todate").options[document.getElementById("drpMonthtxt_todate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_todate").options[document.getElementById("drpYeartxt_todate").selectedIndex].value;
    ps = null;
    retValue = Portal.SODC.IDIS.ListCrimeInfo.GetResult(WebpartId, LocationId, frDate, toDate, CurrentPage, JumpJavaScript, ps).value;
    if (retValue.indexOf("ERROR") != -1) {
        alert(retValue.replace("ERROR: ", ""));
        return;
    }
    document.getElementById("divProcessingSearchResult").innerHTML = "";
    document.getElementById("ContentSearchResult").innerHTML = retValue;
}

// Delete InterCoop Info
function DeleteCrimeInfo(RecordId, LocationId) {
    Msg = Portal.SODC.IDIS.ListCrimeInfo.GetConfirmDeleteMsg().value;
    if (confirm(Msg) == false) return;
    retVal = Portal.SODC.IDIS.ListCrimeInfo.ServerSideDeleteCrimeInfo(RecordId, LocationId).value;
    if (retVal.indexOf("ERROR") != -1) {
        alert(retVal);
        return;
    }
    alert(retVal);
    ClientDoSearchByLocation();
}

function ClientSideExportData() {
    frDate = document.getElementById("drpDaytxt_frdate").options[document.getElementById("drpDaytxt_frdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_frdate").options[document.getElementById("drpMonthtxt_frdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_frdate").options[document.getElementById("drpYeartxt_frdate").selectedIndex].value;
    toDate = document.getElementById("drpDaytxt_todate").options[document.getElementById("drpDaytxt_todate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_todate").options[document.getElementById("drpMonthtxt_todate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_todate").options[document.getElementById("drpYeartxt_todate").selectedIndex].value;
    var LocationId = $("#_locationsel").val();
    if (frDate == "//") {
        frDate = null;
    }
    if (toDate == "//") {
        toDate = null;
    }

    result = document.getElementById("ContentSearchResult");
    if (LocationId) {
        document.getElementById("divProcessingSearchResult").innerHTML = "Hệ thống đang xử lý...";
        Ret = Portal.SODC.IDIS.ListCrimeInfo.ServerExportData(LocationId, frDate, toDate).value;
        if (Ret.indexOf("ERROR") != -1) {
            alert(Ret.replace("ERROR: ", ""));
            document.getElementById("divProcessingSearchResult").innerHTML = "";
            return;
        } else {
            document.getElementById("divProcessingSearchResult").innerHTML = "";
            window.open(Ret, "_blank");
        }
    } else {
        alert("Chưa xác định được địa phương !!!");
        result.innerHTML = "";
    }
}